/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classe.controlador;

import classe.controlador.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import classe.entidade.RequisicaoMaterial;
import classe.entidade.Material;
import classe.entidade.RequisicaoItem;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Kellison
 */
public class RequisicaoItemJpaController implements Serializable {

    public RequisicaoItemJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    
    public RequisicaoItemJpaController() {
        this.emf = Persistence.createEntityManagerFactory("GerenciadorAlmoxarifadoPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(RequisicaoItem requisicaoItem) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RequisicaoMaterial idTblRequisicaoMaterial = requisicaoItem.getIdTblRequisicaoMaterial();
            if (idTblRequisicaoMaterial != null) {
                idTblRequisicaoMaterial = em.getReference(idTblRequisicaoMaterial.getClass(), idTblRequisicaoMaterial.getId());
                requisicaoItem.setIdTblRequisicaoMaterial(idTblRequisicaoMaterial);
            }
            Material idTblMaterial = requisicaoItem.getIdTblMaterial();
            if (idTblMaterial != null) {
                idTblMaterial = em.getReference(idTblMaterial.getClass(), idTblMaterial.getId());
                requisicaoItem.setIdTblMaterial(idTblMaterial);
            }
            em.persist(requisicaoItem);
            if (idTblRequisicaoMaterial != null) {
                idTblRequisicaoMaterial.getRequisicaoItemList().add(requisicaoItem);
                idTblRequisicaoMaterial = em.merge(idTblRequisicaoMaterial);
            }
            if (idTblMaterial != null) {
                idTblMaterial.getRequisicaoItemList().add(requisicaoItem);
                idTblMaterial = em.merge(idTblMaterial);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(RequisicaoItem requisicaoItem) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RequisicaoItem persistentRequisicaoItem = em.find(RequisicaoItem.class, requisicaoItem.getId());
            RequisicaoMaterial idTblRequisicaoMaterialOld = persistentRequisicaoItem.getIdTblRequisicaoMaterial();
            RequisicaoMaterial idTblRequisicaoMaterialNew = requisicaoItem.getIdTblRequisicaoMaterial();
            Material idTblMaterialOld = persistentRequisicaoItem.getIdTblMaterial();
            Material idTblMaterialNew = requisicaoItem.getIdTblMaterial();
            if (idTblRequisicaoMaterialNew != null) {
                idTblRequisicaoMaterialNew = em.getReference(idTblRequisicaoMaterialNew.getClass(), idTblRequisicaoMaterialNew.getId());
                requisicaoItem.setIdTblRequisicaoMaterial(idTblRequisicaoMaterialNew);
            }
            if (idTblMaterialNew != null) {
                idTblMaterialNew = em.getReference(idTblMaterialNew.getClass(), idTblMaterialNew.getId());
                requisicaoItem.setIdTblMaterial(idTblMaterialNew);
            }
            requisicaoItem = em.merge(requisicaoItem);
            if (idTblRequisicaoMaterialOld != null && !idTblRequisicaoMaterialOld.equals(idTblRequisicaoMaterialNew)) {
                idTblRequisicaoMaterialOld.getRequisicaoItemList().remove(requisicaoItem);
                idTblRequisicaoMaterialOld = em.merge(idTblRequisicaoMaterialOld);
            }
            if (idTblRequisicaoMaterialNew != null && !idTblRequisicaoMaterialNew.equals(idTblRequisicaoMaterialOld)) {
                idTblRequisicaoMaterialNew.getRequisicaoItemList().add(requisicaoItem);
                idTblRequisicaoMaterialNew = em.merge(idTblRequisicaoMaterialNew);
            }
            if (idTblMaterialOld != null && !idTblMaterialOld.equals(idTblMaterialNew)) {
                idTblMaterialOld.getRequisicaoItemList().remove(requisicaoItem);
                idTblMaterialOld = em.merge(idTblMaterialOld);
            }
            if (idTblMaterialNew != null && !idTblMaterialNew.equals(idTblMaterialOld)) {
                idTblMaterialNew.getRequisicaoItemList().add(requisicaoItem);
                idTblMaterialNew = em.merge(idTblMaterialNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = requisicaoItem.getId();
                if (findRequisicaoItem(id) == null) {
                    throw new NonexistentEntityException("The requisicaoItem with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RequisicaoItem requisicaoItem;
            try {
                requisicaoItem = em.getReference(RequisicaoItem.class, id);
                requisicaoItem.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The requisicaoItem with id " + id + " no longer exists.", enfe);
            }
            RequisicaoMaterial idTblRequisicaoMaterial = requisicaoItem.getIdTblRequisicaoMaterial();
            if (idTblRequisicaoMaterial != null) {
                idTblRequisicaoMaterial.getRequisicaoItemList().remove(requisicaoItem);
                idTblRequisicaoMaterial = em.merge(idTblRequisicaoMaterial);
            }
            Material idTblMaterial = requisicaoItem.getIdTblMaterial();
            if (idTblMaterial != null) {
                idTblMaterial.getRequisicaoItemList().remove(requisicaoItem);
                idTblMaterial = em.merge(idTblMaterial);
            }
            em.remove(requisicaoItem);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<RequisicaoItem> findRequisicaoItemEntities() {
        return findRequisicaoItemEntities(true, -1, -1);
    }

    public List<RequisicaoItem> findRequisicaoItemEntities(int maxResults, int firstResult) {
        return findRequisicaoItemEntities(false, maxResults, firstResult);
    }

    private List<RequisicaoItem> findRequisicaoItemEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(RequisicaoItem.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public RequisicaoItem findRequisicaoItem(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(RequisicaoItem.class, id);
        } finally {
            em.close();
        }
    }

    public int getRequisicaoItemCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<RequisicaoItem> rt = cq.from(RequisicaoItem.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
